#ifndef RANDOMCHART_H
#define RANDOMCHART_H

#include <QObject>
#include <QChar>
#include <QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QSplineSeries>
#include <QValueAxis>
#include <QTimer>
#include <QTime>
#include <QDateTime>
#include <QDateTimeAxis>
#include <QValueAxis>
#include <QSplineSeries>

#include <QRandomGenerator>
class RandomChart : public QObject
{
    Q_OBJECT
public:
    explicit RandomChart(QObject *parent = nullptr);
    RandomChart(QChart *chart) {
           // 初始化图表系列
           series = new QLineSeries();

           // 将系列添加到图表
           chart->addSeries(series);

           // 设置图表的标题和轴标题
           chart->setTitle("Random Numbers Over Time");
           chart->setAxisTitle(QChart::AxisX, "Time");
           chart->setAxisTitle(QChart::AxisY, "Random Value");

           // 创建定时器
           timer = new QTimer(this);
           connect(timer, &QTimer::timeout, this, &RandomChart::onTimeout);
           timer->start(1000); // 每秒触发一次

           // 配置图表视图
           chartView = new QChartView(chart);
           chartView->setRenderHint(QPainter::Antialiasing);
       }
signals:
    void onTimeout() {
            // 获取当前时间
            QDateTime currentTime = QDateTime::currentDateTime();

            // 生成随机数
            int randomValue = QRandomGenerator::global()->bounded(101);

            // 向系列添加新的数据点
            series->append(currentTime, randomValue);

            // 滚动图表视图
            if (series->count() > 50) { // 保持最近50个数据点
                series->remove(0, series->count() - 50);
            }
        }

private:
    QLineSeries *series;
       QTimer *timer;
       QChartView *chartView;


};

#endif // RANDOMCHART_H
